"""
run "manage.py test".
"""

from django.test import TestCase
from models import Person, RegRecord, make_sym_matrix

class RegTestCase(TestCase):
    def all_equal(self, *pairs):
        for p in pairs:
            self.assertEqual(*p)

    def setUp(self):
        self.bob = Person(
            first_name = 'Bob',
            gender = 'M',
            age = 42,
            seeking_primary = True,
            kinky = False,
            seek_gender = 'F',
            seek_age_min = 18,
            seek_age_max = 42,
            seek_kinkiness = 'EI')
        self.bob.save()

        self.bob_reg = RegRecord(
            is_group = False,
            location = 'SF',
        )
        self.bob_reg.save()

        self.bob_reg.people.add(self.bob)

        self.lulu = Person(
            gender = 'F',
            age = 42,
            seeking_primary = False,
            kinky = True,
            seek_gender = 'M,F,TM,TF,Q',
            seek_age_min = 24,
            seek_age_max = 60,
            seek_kinkiness = 'EI')
        self.lulu.save()

        self.lulu_reg = RegRecord(
            is_group = False,
            location = 'SF,EB',
        )
        self.lulu_reg.save()

        self.lulu_reg.people.add(self.lulu)

    def tearDown(self):
        self.bob_reg.delete()
        self.lulu_reg.delete()
        self.bob.delete()
        self.lulu.delete()

    def test_Bob(self):
        bob = self.bob
        self.all_equal(
            (bob.first_name, 'Bob'),
            (bob.gender_set, set('M')),
            (bob.seek_gender_set, set('F')),
            (bob.will_date_kinky, True),
            (bob.will_date_nonkinky, True),
        )

    def test_Bob_reg(self):
        r = self.bob_reg
        self.all_equal(
            (r.integrity_ok(), True),
            (r.indiv, self.bob),
            (r.is_group, False),
            (r.genders, set('M')),
            (r.has_gender('M'), True),
            (r.has_gender('F'), False),
            (r.wants_gender('M'), False),
            (r.wants_gender('F'), True),
            (r.wants_mf, False),
            (r.is_a_male, True),
            (r.treat_as_female, False),
            (r.location_set,set(['SF'])),
        )

    def test_Lulu_reg(self):
        r = self.lulu_reg
        self.all_equal(
            (r.integrity_ok(), True),
            (r.indiv, self.lulu),
            (r.is_group, False),
            (r.genders, set('F')),
            (r.has_gender('M'), False),
            (r.has_gender('F'), True),
            (r.wants_gender('M'), True),
            (r.wants_gender('F'), True),
            (r.wants_mf, True),
            (r.treat_as_female, True),
            (r.location_set,set(['SF','EB'])),
        )


    def test_person_interest(self):
        bob = self.bob
        lulu = self.lulu
        self.assertTrue(bob.will_date(lulu))
        self.assertFalse(bob.will_date(bob))
        self.assertTrue(lulu.will_date(lulu))
        self.assertTrue(lulu.will_date(bob))
        self.assertEqual(bob.interest_score(lulu), 1)
        self.assertEqual(lulu.interest_score(bob), 3)
        self.assertEqual(lulu.interest_score(lulu), 5)

    def test_record_interest(self):
        bob = self.bob_reg
        lulu = self.lulu_reg
        self.assertEqual(lulu.ok_gay_match(bob),False)
        self.assertEqual(lulu.ok_match(bob, 'gay'),False)
        self.assertFalse(bob.mf_gender_match(lulu))
        self.assertTrue(bob.mf_gender_cross(lulu))
        self.assertFalse(lulu.mf_gender_match(bob))
        self.assertTrue(lulu.mf_gender_cross(bob))
        self.assertTrue(bob.location_overlap(bob))
        self.assertTrue(bob.location_overlap(lulu))
        self.assertEqual(bob.interest_score(bob),0)
        self.assertEqual(bob.interest_score(lulu),3)
        self.assertEqual(lulu.interest_score(bob),5)
        self.assertEqual(lulu.interest_score(lulu),7)
        self.assertTrue(bob.ok_match(lulu, 'all'))
        self.assertTrue(bob.ok_match(lulu, 'str'))
        self.assertTrue(bob.ok_match(lulu, 'gay'))
        self.assertTrue(lulu.ok_match(bob, 'all'))
        self.assertTrue(lulu.ok_match(bob, 'str'))
        self.assertFalse(lulu.ok_match(bob, 'gay'))
        self.assertEqual(bob.matrix_score(lulu, 'all'), 3)
        self.assertEqual(lulu.matrix_score(bob, 'all'), 5)
        self.assertEqual(lulu.matrix_score(bob, 'gay'), 0)

    def test_matrices(self):
        gm,sm,am = [make_sym_matrix(t) for t in ('gay','str','all')]
        bob = self.bob_reg
        lulu = self.lulu_reg
        for m in (gm,sm,am):
            self.assertEqual(m[bob,lulu],m[lulu,bob])
        self.assertEqual(am[bob,lulu],3)
        self.assertEqual(sm[bob,lulu],3)
        self.assertEqual(gm[bob,lulu],0)
